package app.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import app.config.Config;
import app.dao.AccountDao;
import app.dao.WorkerDao;
import app.domain.Account;
import app.domain.Worker;
import app.service.WorkManageService;
import app.utils.MD5Util;


@Service
public class WorkManageServiceImpl implements WorkManageService{

	@Autowired
    private WorkerDao workManageDao;
	@Autowired
	private AccountDao accountDao;
	
	@Transactional
	@Override
	public void singleImport(Worker work) {
		//创建工人帐号
		Account account=new Account();
		account.setAccountName(work.getTel());
		account.setPassword(MD5Util.MD5(work.getTel().trim()+Config.WORKER_DEFAULT_PASSWORD));
		account.setRole(Config.ROLE_WORKER);
		accountDao.addAccount(account);
		
		work.setId(account.getId());
		workManageDao.singleImport(work);
	}
	@Override
	public List<Worker> findAllWork() {
		// TODO Auto-generated method stub
		return workManageDao.findAllWork();
	}
	@Transactional
	@Override
	public int analyzeExcel(File file) {
		
		// TODO Auto-generated method stub
		ArrayList<Worker> works = new ArrayList<Worker>();
		
		XSSFWorkbook workbook = null;
		try {
			//创建Excel,读取文件内容
			workbook = new XSSFWorkbook(FileUtils.openInputStream(file));
			XSSFSheet sheet = workbook.getSheetAt(0);	
			int lastRowNum=sheet.getLastRowNum();
			for (int i = 1; i <= lastRowNum; i++) {
				XSSFRow row = sheet.getRow(i);
				Worker work = new Worker();
				work.setWorkId(Integer.parseInt(getXCellValue(row.getCell(0))));
				work.setName(getXCellValue(row.getCell(1)));
				if(getXCellValue(row.getCell(2)).equals("男")){
					work.setSex(1);
				}else{
					work.setSex(2);
				}			
				work.setTel(getXCellValue(row.getCell(3)));
				work.setSkill(getXCellValue(row.getCell(4)));
				//创建工人帐号
				Account account=new Account();
				account.setAccountName(work.getTel());
				account.setPassword(MD5Util.MD5(work.getTel().trim()+Config.WORKER_DEFAULT_PASSWORD));
				account.setRole(Config.ROLE_WORKER);
				accountDao.addAccount(account);
				
				work.setId(account.getId());
				works.add(work);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
			
		}	
		workManageDao.batchImport(works);
		return 1;
	}
	
	public String getXCellValue(XSSFCell cell) {  
        String cellValue = "";  
        DecimalFormat df = new DecimalFormat("#");  
        switch (cell.getCellType()) {  
        case XSSFCell.CELL_TYPE_STRING:  
            cellValue = cell.getRichStringCellValue().getString().trim();  
            break;  
        case XSSFCell.CELL_TYPE_NUMERIC:  
            cellValue = df.format(cell.getNumericCellValue()).toString();  
            break;  
        case XSSFCell.CELL_TYPE_BOOLEAN:  
            cellValue = String.valueOf(cell.getBooleanCellValue()).trim();  
            break;  
        case XSSFCell.CELL_TYPE_FORMULA:  
            cellValue = cell.getCellFormula();  
            break;  
        default:  
            cellValue = "";  
        }  
        return cellValue;  
    }
	@Override
	public Worker findWorkById(int id) {
		return workManageDao.getById(id);
	}
	@Override
	public void updateWorkById(Worker work) {
		// TODO Auto-generated method stub
		workManageDao.updateWorkById(work);
	}
	@Override
	public void deleteWorkById(Integer id) {
		// TODO Auto-generated method stub
		workManageDao.deleteWorkById(id);
	}
	@Override
	public String findNameByOrderId(Integer orderId) {
		// TODO Auto-generated method stub
		return workManageDao.findNameByOrderId(orderId);
	}  
}
